﻿Public Class FrmABMEmpleado
    Dim acceso As New acceso_a_datos
    Dim grillaActiva As Boolean = False
    Private Sub FrmABMEmpleado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        cargarGrilla()
        Me.DataGridViewDatos.Focus()

    End Sub

    Private Sub cargarGrilla()

        Dim consulta As String = "select Legajo, Nombre , Apellido, Telefono from Empleado"
        Me.DataGridViewDatos.DataSource = acceso._consulta(consulta)
        Me.GroupBoxDatos.Enabled = True
        Me.DataGridViewDatos.Enabled = True
        btnEliminar.Enabled = True
        btnModificar.Enabled = True
        Me.DataGridViewDatos.Focus()

    End Sub
    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click

        accion = estado.nuevo
        deshabilitarGrilla()
        Me.GroupBoxDatos.Enabled = True

    End Sub

    Private Sub deshabilitarGrilla()

        grillaActiva = False
        DataGridViewDatos.Enabled = False
        btnEliminar.Enabled = False
        btnModificar.Enabled = False

    End Sub
    Private Function validarCampos() As Boolean

        Dim valido As Boolean = True
        Dim camposVacios(GroupBoxDatos.Controls.Count) As Control

        For p = 0 To camposVacios.Length - 1
            camposVacios(p) = Nothing
        Next
        For Each campo As Control In GroupBoxDatos.Controls
            If Not campo.GetType.Name = "Panel" And campo.Text = "" Then
                camposVacios(campo.TabIndex) = campo
                valido = False
            End If
        Next

        Dim campoFoco As Control = Nothing
        Dim mensajeCamposVacios As String = "Los campos "

        For i = 0 To camposVacios.Length - 1
            If Not camposVacios(i) Is Nothing Then
                mensajeCamposVacios += camposVacios(i).Tag & ", "
                If campoFoco Is Nothing Then
                    campoFoco = camposVacios(i)
                End If
            End If
        Next

        mensajeCamposVacios += "están vacíos."

        If Not valido Then
            MsgBox(mensajeCamposVacios, MsgBoxStyle.Exclamation, "Campos no Válidos")
        End If

        If Not campoFoco Is Nothing Then
            campoFoco.Focus()
        End If

        Return valido

    End Function
    Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click

        Dim consultaInsert As String

        If validarCampos() Then
            If Me.validarLegajo = True Then
                acceso._tabla = "Empleado"
                consultaInsert = "INSERT INTO " & acceso._tabla & "(Legajo, Nombre, Apellido, Telefono)"
                consultaInsert &= "VALUES ( " & Me.TxtLegajo.Text
                consultaInsert &= ", '" & Me.txtNombre.Text & "'"
                consultaInsert &= ", '" & Me.txtApellido.Text & "'"
                consultaInsert &= ", " & Me.txtTelefono.Text
                consultaInsert &= ")"

                Dim consultaModificar As String

                If accion = estado.nuevo Then
                    acceso._consulta(consultaInsert)
                    MsgBox("Se agregó el nuevo Empleado " & Me.txtNombre.Text & " con éxito")
                Else
                    consultaModificar = "UPDATE " & acceso._tabla
                    consultaModificar &= " SET Legajo = " & Me.TxtLegajo.Text
                    consultaModificar &= " , Nombre = '" & Me.txtNombre.Text & "'"
                    consultaModificar &= " , Apellido = '" & Me.txtApellido.Text & "'"
                    consultaModificar &= " , Telefono = " & Me.txtTelefono.Text
                    consultaModificar &= " WHERE Legajo = " & Me.DataGridViewDatos.CurrentRow.Cells(0).Value
                    acceso._modificar(consultaModificar)
                    MsgBox("Se modificó el Empleado" & Me.txtNombre.Text & " con éxito")
                End If
                habilitarDeshabilitarBotonesAcciones()
                habilitarDeshabilitarCampos()
                Me.DataGridViewDatos.Enabled = True
                Me.cargarGrilla()
                limpiarCampos()
                Me.DataGridViewDatos.Focus()
                btnEliminar.Enabled = True
                btnModificar.Enabled = True
            End If
        End If

    End Sub
    Private Function validarLegajo() As Boolean

        Dim resultado As Boolean = True

        If Me.TxtLegajo.Text > 0 Then
            resultado = True
        Else
            resultado = False
            MsgBox("El valor del legajo a cero")
        End If
        Return resultado

    End Function
    Private Sub borrarContenidoEnGrilla()

        DataGridViewDatos.DataSource = Nothing

    End Sub
    Private Sub btnModificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificar.Click

        accion = estado.modificar
        deshabilitarGrilla()

    End Sub
    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click

        Me.DataGridViewDatos.Enabled = True
        Me.btnEliminar.Enabled = True
        Me.btnModificar.Enabled = True
        Me.btnNuevo.Enabled = True

    End Sub
    Private Sub cargarCampos()

        Dim acceso As New acceso_a_datos

        If Not Me.DataGridViewDatos.CurrentRow Is Nothing Then

            Me.txtLegajo.Text = Me.DataGridViewDatos.CurrentRow.Cells("Legajo").Value
            Me.txtNombre.Text = Me.DataGridViewDatos.CurrentRow.Cells("Nombre").Value
            Me.txtApellido.Text = Me.DataGridViewDatos.CurrentRow.Cells("Apellido").Value
            Me.txtTelefono.Text = Me.DataGridViewDatos.CurrentRow.Cells("Telefono").Value
        End If

    End Sub
    Friend Sub resultadoBusqueda(ByRef _tabla As Data.DataTable)

        Me.DataGridViewDatos.DataSource = _tabla
        Dim activo As Boolean = False

        If Me.DataGridViewDatos.Rows.Count > 0 Then
            Me.DataGridViewDatos.CurrentCell = DataGridViewDatos(1, 0)
            activo = True
        End If

        Me.DataGridViewDatos.Enabled = activo
        btnModificar.Enabled = activo
        btnEliminar.Enabled = activo
        grillaActiva = True

        For i = 4 To 10
            DataGridViewDatos.Columns(i).Visible = False
        Next
        If activo Then
            cargarCampos()
        Else
            limpiarCampos()
        End If
        Me.DataGridViewDatos.Focus()

    End Sub
    Private Sub txtLegajo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)

        If Char.IsDigit(e.KeyChar) Then
            e.Handled = False
        ElseIf Char.IsControl(e.KeyChar) Then
            e.Handled = False
        Else
            e.Handled = True
        End If

    End Sub
    Private Sub txtTelefono_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)

        If Char.IsDigit(e.KeyChar) Then
            e.Handled = False
        ElseIf Char.IsControl(e.KeyChar) Then
            e.Handled = False
        Else
            e.Handled = True
        End If

    End Sub
    Private Sub btnEliminar_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click

        Dim tabla As New Data.DataTable
        Dim consulta As String = "select e.Legajo, a.Legajo, v.Legajo from Empleado e, Alquiler a, Venta v  where e.Legajo = a.Legajo and e.Legajo = v.Legajo and e.Legajo = " & DataGridViewDatos.CurrentRow.Cells(0).Value
        tabla = acceso._consulta(consulta)

        If tabla.Rows.Count > 0 Then
            MsgBox("No se puede eliminar El empleado con ventas o alquileres asociados")
            Exit Sub
        End If

        Dim titulo As String = Me.DataGridViewDatos.CurrentRow.Cells(1).Value
        acceso._tabla = "Empleado"

        If MsgBox("Está seguro que desea borrar el empleado " & titulo & " ?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Importante") = MsgBoxResult.Yes Then
            Me.acceso._borrar("Delete From Empleado where Legajo = " & DataGridViewDatos.CurrentRow.Cells(0).Value)
            MsgBox("Se borró el empleado " & titulo & " con éxito")
            accion = estado.eliminar
            limpiarCampos()
            Me.cargarGrilla()
        End If

    End Sub
    Private Sub DataGridViewDatos_SelectionChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridViewDatos.SelectionChanged

        cargarCampos()

    End Sub

End Class
